<?php
/********************************************************
 *
 * 此文件的作用:
 *		包括勤助正式员工的所有操作
 *
 * @copyright Copyright (c) 2008-2009
 * @author 刘昌功 lcg635@126.com
 *
 ********************************************************/
defined('APP_DIR') or header("HTTP/1.0 404 Not Found");

FLEA::loadClass('Common_Model');

class Model_Worker extends Common_Model {

	function __construct() {
		parent::__construct();
		$this->_tb = FLEA::getSingleton('Table_Worker');
	}
	
	function getAllWorkers($page = 0, $pageSize = 15) {
		FLEA::loadHelper('pager');
		$_modUser = FLEA::getSingleton('Model_user');
		$_tbWorker = FLEA::getSingleton('Table_Worker');

		$pager = new FLEA_Helper_Pager($_tbWorker, $page, $pageSize);
		$lists = $pager->findAll();
		foreach ($lists as $k => $v) {
			$lists[$k]['user'] = $_modUser->getUserInfo($v['user_id']);
		}
		$data = array(
			'lists' => $lists,
			'pagerData' => $pager->getPagerData()
		);
		return $data;
	}
	
	function getList() {
		$result = $this->_tb->findAll();
		return $result;
	}

	function getWorkerInfo($uid) {
		$data = $this->_tb->find('user_id = ' . $uid);
		//$data['created'] = date('Y-m-d',time());
		return $data;
	}
	
	function search($data = NULL ,$page = 0, $pageSize =20) {
		$_modUser = FLEA::getSingleton('Model_User');
		if($data == null && !empty($_COOKIE['workerSql']))
			$query = $_COOKIE['workerSql'];
		else {
			//构造sql语句
			$que = "select *  from user, worker where user.user_id = worker.user_id ";
			//开始和结束日期
			$que.=' and ( worker.created >\''.$data['startDate'].'\') and ( worker.created <\''.$data['endDate'].'\')';
			$query = '';
			$condition = array(
				array($data['trueName'],'user.truename' , 'like' ,'\'%'.$data['trueName'].'%\''),//搜索姓名
				//array($data['sex2'] ,'user.sex' , '=' ,$data['sex2']),							//搜索性别
				//array($data['num'] ,'user.num' , 'like' ,'\'%'.$data['num'].'%\''),						//搜索学号
				//array($data['college'] ,'user.college' , 'like' ,'\'%'.$data['college'].'%\''),	//搜索学院
				//array($data['major'] ,'user.major' , 'like' ,'\'%'.$data['major'].'%\''),		//搜索专业
				array($data['poor'] ,'user.poor' , '=' ,$data['poor']),							//搜索贫困生
				array($data['userType'] ,'user.userType' , '=' ,$data['userType'])			//搜索层次
			);
	
			for ($i=0;$i < count($condition);$i++){
				if (null != $condition[$i][0] && -1 != $condition[$i][0]) {
					for ($j = 1,$query_j = '' ;$j <count($condition[$i]);$j++){
						$query_j .= ' '. $condition[$i][$j];
					}
				$query .= ' and '.$query_j;
				}						
			}
			//搜索学院
			if($data['college']|| $data['college2']){
			$query_college =' and ( user.college like \'%'.$data['college'].'%\')';
			if($data['college2']) $query_college =' and ( user.college like \'%'.$data['college2'].'%\')';
			$query .= $query_college;
			}
			//搜索系
			if($data['department'] || $data['department2']){
				$query_department =' and ( user.department like \'%'.$data['department'].'%\' or user.major like \'%'.$data['department'].'%\')';
				if($data['department2']) $query_department =' and ( user.department like \'%'.$data['department2'].'%\' or user.major like \'%'.$data['department2'].'%\')';
				$query .=$query_department;
			}
			//搜索专业
			if( $data['major'] || $data['major2']){
				$query_major =' and ( user.department like \'%'.$data['major'].'%\' or user.major like \'%'.$data['major'].'%\')';
				if($data['major2']){
					$query_major =' and ( user.department like \'%'.$data['major2'].'%\' or user.major like \'%'.$data['major2'].'%\')';
				}
				$query .= $query_major;
			}
			//搜索籍贯
			if($data['province'])
			$query .=' and ( user.province like \'%'.$data['province'].'%\')';
			//搜索所报部门
			if($data['group'])
			$query .=' and ( worker.group = '.$data['group'].')';
			//搜索所报部门之办公室部门
			if($data['office'])
			$query .=' and ( worker.office like \'%'.$data['office'].'%\')';
			//整合搜索
			if($data['zhenghe']){
				$query .=' and ( user.trueName like \'%'.$data['zhenghe'].'%\'';
				$query .=' or user.province like \'%'.$data['zhenghe'].'%\'';
				$query .=' or user.college like \'%'.$data['zhenghe'].'%\'';
				$query .=' or user.department like \'%'.$data['zhenghe'].'%\'';
				$query .=' or user.major like \'%'.$data['zhenghe'].'%\'';
				$query .=')';
			}
			$query = $que.$query;
			//dump($query);
			setcookie('workerSql',$query);
			//$_SESSION['sForm'] = serialize($data);
		}

		//查询
		$result = $this->_tb->execute($query);
		while ($row = mysql_fetch_assoc($result)) {
			$workers['lists'][] = $row;
		}

		foreach ($workers['lists'] as $k => $v) {
			$workers['lists'][$k]['user'] = $_modUser->getUserInfo($v['user_id']);
		}
		$total = count($workers['lists']);
		$searchResult['resultCount']= '搜索到符合条件的员工记录'.$total.'条';

		$totalCount = count($workers['lists']);
		$last = ($page+1) *($pageSize) >= $totalCount ? $totalCount : ($page+1) *($pageSize);
			
		for($i = $page * $pageSize ; $i < $last ; $i++ )
			$searchResult['lists'][]  = $workers['lists'][$i];
		//分页
		FLEA::loadHelper('pager');
		$pager = new FLEA_Helper_Pager($_tbWorker, $page, $pageSize);
		$pager->setCount($totalCount);
		$searchResult['pagerData'] = $pager->getPagerData();
		return $searchResult;
	}
	
	function searchrealWorkers($data = NULL ,$page = 0, $pageSize =10,$type = null) {
		$_modUser = FLEA::getSingleton('Model_User');
		//开始和结束日期
		$data['startDate'] = '2000-09-01';
		$data['endDate'] = '2008-09-30';
		$query.='select *  from user, worker where user.user_id = worker.user_id
		 and ( worker.created >\''.$data['startDate'].'\') and ( worker.created <\''.$data['endDate'].'\')';		
		if($type == '待审核'){
				$query .=' and ( worker.processresult = "待审核" or worker.processresult = "" )';
		}else {
			$query .=' and ( worker.processresult ="'.$type.'")';
		}

		dump($query);
		//查询
		$result = $this->_tb->execute($query);
		while ($row = mysql_fetch_assoc($result)) {
			$workers['lists'][] = $row;
		}

		foreach ($workers['lists'] as $k => $v) {
			$workers['lists'][$k]['user'] = $_modUser->getUserInfo($v['user_id']);
		}
		$total = count($workers['lists']);
		$searchResult['resultCount']= '搜索到符合条件的员工记录'.$total.'条';

		$totalCount = count($workers['lists']);
		$last = ($page+1) *($pageSize) >= $totalCount ? $totalCount : ($page+1) *($pageSize);
			
		for($i = $page * $pageSize ; $i < $last ; $i++ )
			$searchResult['lists'][]  = $workers['lists'][$i];
		//分页
		FLEA::loadHelper('pager');
		$pager = new FLEA_Helper_Pager($_tbWorker, $page, $pageSize);
		$pager->setCount($totalCount);
		$searchResult['pagerData'] = $pager->getPagerData();
		return $searchResult;
	}

	function hasRegister($uid) {
		return $this->_tb->findCount('user_id = ' . $uid);
	}
	
	function getSeri($college,$grade){
		$_modUser = FLEA::getSingleton('Model_User');
		$query = "select *  from user, worker where user.user_id = worker.user_id "
		.' and ( user.college like \'%'.$college.'%\')'.' and ( user.num like \''.$grade.'%\')'
		.' and ( worker.created >\'2008-9-19\')';
		//dump($query);
		$result = $this->_tb->execute($query);
		while ($row = mysql_fetch_assoc($result)) {
			$workers['lists'][] = $row;
		}

		foreach ($workers['lists'] as $k => $v) {
			$workers['lists'][$k]['user'] = $_modUser->getUserInfo($v['user_id']);
		}
		$total = count($workers['lists']);
		$searchResult['resultCount']= '搜索到符合条件的员工记录'.$total.'条';

		return $total;
	}

	function register($data, $type='register', $userData) {
		$result = array();
		if ( !is_array($data) ) {
			$result[] = '提交的数据为空！';
			return $result;
		}
		//if( $this->_tb->findCount('user_id = ' . $data['user_id']) > 0 )
		//确定、取消
		//return false;

		$course = '';
		foreach ( $data['course'] as $v )
			$course .= '|' . $v;
		$data['course'] = substr($course, 1);
		
		$office = '';
		foreach ($data['office'] as $o)
			$office .= '|' .$o;
		//if($office.count() > 2) 
		$data['office'] = substr($office, 1);
			
		
		//处理图书馆具体职位
		if($data['group'] == 7 && $data['lib'])
		$data['group'] = $data['lib'];
		
		//开始编号
		//a、第一位表示是勤工助学管理委员会的同学，用Q表示
		//b、第二位表示学生所坐在学院，各学院表示如下：
		switch ($userData['college']) {
			case '文学院':$b='WX';break;
			case '外国语学院':$b='WG';break;
			case '新闻与传播学院':$b='XW';break;
			case '理工学院':$b='LG';break;
			case '信息科学技术学院':$b='XX';break;
			case '生命科学技术学院':$b='SM';break;
			case '经济学院':$b='JJ';break;
			case '法学院':$b='FX';break;
			case '药学院':$b='YP';break;
			case '管理学院':$b='GL';break;
			case '医学院':$b='YX';break;
			case '华文学院':$b='HW';break;
			case '深圳旅游学院':$b='SZ';break;
			case '知识产权学院':$b='ZS';break;
			case '国际学院':$b='GJ';break;
			case '艺术学院':$b='YS';break;
			case '教育学院':$b='JY';break;
			case '第一临床医学院':$b='ⅠL';break;//注意：major.php中无此学院，请明确
			case '第二临床医学院':$b='ⅡL';break;//注意：major.php中无此学院，请明确
				
			default:$b='**';	break;
		}
		//假如是某个学院的研究生就在其学院编号后面加上Y
		if($userData['userType'] == '研究生') $b.='Y';//userType需要明确
		//c、第三位表示填写存档资料的年月，由4位数字组成。如某学生是在2007年9月填写的资料就是0709
		$c = date('ym',time());
		//d、第四位表示学生的年级由2位数字组成。如某学生是06的就写上06
		$d = $userData['num'][2].$userData['num'][3] == ''?'**':$userData['num'][2].$userData['num'][3];
		//e、第五位由两个数字组成，由处理资料的同事视情况而定，定义填写同学是在资料库中存档的第几位同学。
		if($userData['seri'])$e = $userData['seri'];
			else $e='***';
		//f、最后一位是表示他所在的岗位的，也就是说只有在直辖或非直辖岗位上由岗位才有的,见下一函数addWorkNmu
		$f = $userData['state'];//此处应由人力部决定，请明确
		
		$data['number'] = 'Q'.$b.$c.$d.$e.$f;
		$data['created'] = date('Y-m-d',time());
		//dump($data);
		$result = $type == 'register'? $this->_tb->create($data):$this->_tb->update($data);
		return $result;
	}
	
	function addWorkNum($workerData = null,$groupData = null,$resultData =null){
		if($data['result'] == '正式员工' && $data['group']){
		//f、最后一位是表示他所在的岗位的，也就是说只有在直辖或非直辖岗位上由岗位才有的
		//if($workerData['state'] != $groupData){
		if(1){
				if(($workerData['state'] == '物业服务部' || $workerData['state'] == '综合服务部' || $workerData['state'] == '市场部' 
									||	$workerData['state'] == '财务部' || $workerData['state'] == '人力部' ||$workerData['state'] == '推广部') 
					&& ($groupData == '物业服务部' || $groupData == '综合服务部' || $groupData == '市场部' 	||	$groupData == '财务部'
					 || $groupData == '人力部' ||$groupData == '推广部')){
												
						;
				}else {
					switch ($groupData) {
						case '课室服务组':$f='K';break;
						case '饭堂服务组':$f='F';break;
						case '多媒体教室管理组':$f='D';break;
						case '粤港澳青年交流中心':$f='Y';break;
						case '金四管理组':$f='JS';break;
						case '机关部处':$f='JG';break;
						case '学生活动中心管理组':$f='X';break;
						case '建阳服务组':$f='J';break;
						case '图书馆':$f='T';break;
						
						case '物业服务部':
						case '综合服务部':
						case '市场部':
						case '财务部':
						case '人力部':
						case '推广部':$f='B';break;
						default:$f='';	break;
					}
					$workerData['number'] .=$f;
				}
			}
		}
		$workerData['state'] =$groupData;
		$workerData['processresult'] =$resultData;
		$workerData['processgroup'] =$groupData;

		//dump($workerData);
		$result = $this->_tb->update($workerData);
		return $result;
	}
	
	function updateWorkNum($workerData = null,$number = null){
		$workerData['number'] =$number;

		//dump($workerData);
		$result = $this->_tb->update($workerData);
		return $result;		
	}

	function getForm($data = null,$data2 = null) {
		$form = $this->_tb->form($data,$data2);
		return $form;
	}
	
	function searchForm() {
		$form = $this->_tb->searchForm();
		return $form;
	}
	
	function connectForm($data = null) {
		$form = $this->_tb->connectForm();
		return $form;
	}
}
